/**
 * 负责利用传入的FeatureLayer图层进行空间查询操作，并返回查询结果
 */
define([
        "esri/views/2d/draw/Draw",
        "esri/geometry/Polygon",
        "esri/layers/GraphicsLayer",
        "esri/Graphic",
        "dojo/_base/declare"],
    function (Draw,Polygon,GraphicsLayer,Graphic,declare) {
        return declare(null, {
            drawObj:null,
            mapView :null,
            tempDrawLayer:null,
            options:{
            },
            /**
             * 获取素有通用的元素
             */
            elements:function(){

            },
            constructor: function (mapView,options) {
                var that=this;
                this.mapView=mapView;
                that.clearAllResult();

                that.options = $.extend(that.options, options);//继承原来默认设置，覆盖属性名称相同的设置
                that.tempDrawLayer=that.mapView.map.findLayerById("tempDrawLayer");
                if(that.tempDrawLayer==null){
                    that.tempDrawLayer = new GraphicsLayer({
                        id:"tempDrawLayer"
                    });
                    var index=that.mapView.map.layers.length+1;
                    that.mapView.map.add(that.tempDrawLayer,index);
                }


            },

            /**
             * 画面
             */
            drawPolygon:function (onCompleted) {
                var that=this;
                //激活画面事件
                if(that.drawObj!=null){
                    return;
                }
                var draw = new Draw({
                    view: that.mapView
                });
                that.drawObj=draw;
                // draw.reset();
                // var action = draw.create("polyline", {mode: "click"});
                var action = draw.create("polygon");
                that.mapView.focus();
                action.on("vertex-add", function (evt) {
                    that.tempDrawLayer.removeAll();
                    var vertices = evt.vertices;
                    var polygon = that.createPolygon(vertices);;
                    var graphic = that.createGraphic(polygon);
                    graphic.attributes = dojo.mixin(graphic.attributes, {
                        randomId: Math.random()
                    });
                    that.tempDrawLayer.add(graphic);
                });
                action.on("cursor-update", function (evt) {
                    that.tempDrawLayer.removeAll();
                    var vertices = evt.vertices;
                    var polygon = that.createPolygon(vertices);;
                    var graphic = that.createGraphic(polygon);
                    graphic.attributes = dojo.mixin(graphic.attributes, {
                        randomId: Math.random()
                    });
                    that.tempDrawLayer.add(graphic);
                });
                action.on("draw-complete", function (evt) {
                    var polygon = that.createPolygon(evt.vertices);
                    // var graphic = that.createGraphic(polygon);
                    // that.mapView.graphics.add(graphic);
                    if(typeof onCompleted=="function"){
                        onCompleted(polygon);
                    }
                    that.drawObj=null;
                });

            },

            /**
             * 通过编辑的节点构建面
             * @param vertices
             * @returns {*}
             */
            createPolygon: function(vertices) {
                var that=this;
                return new Polygon({
                    rings: vertices,
                    spatialReference: that.mapView.spatialReference
                });
            },

            /**
             * 生成Graphic
             * @param polygon
             * @returns {*}
             */
            createGraphic:function(polygon) {
                var graphic = new Graphic({
                    geometry: polygon,
                    symbol: {
                        type: "simple-fill", // autocasts as SimpleFillSymbol
                        color: [178, 102, 234, 0.8],
                        style: "solid",
                        outline: { // autocasts as SimpleLineSymbol
                            color: "#FD8045",
                            width: 1
                        }
                    }
                });
                return graphic;
            },

            /**
             * 清除所有查询结果
             */
            clearAllResult:function () {
                var that=this;
                var  tempDrawLayer=that.mapView.map.findLayerById("tempDrawLayer");
                if (tempDrawLayer!=null) {
                    tempDrawLayer.graphics.removeAll();
                    tempDrawLayer.removeAll();
                    // that.mapView.map.remove(tempDrawLayer);
                }

            },

        });
    });



